package cn.school.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.school.entity.Grade;
import cn.school.service.Service2;

/**
 * Servlet implementation class GradeAjaxServlet
 */
@WebServlet("/GradeAjaxServlet")
public class GradeAjaxServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GradeAjaxServlet() {
		super();
	}

	Service2 sercice = new Service2();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// response.getWriter().append("Served at: ").append(request.getContextPath());

		response.setContentType("application/json;charset=utf-8");
		List<Grade> list = sercice.findALL();
		StringBuilder sb = new StringBuilder();
		sb.append("[");
		for (int i = 0; i < list.size(); i++) {
			Grade g = list.get(i);
			sb.append("{");
			sb.append("\"id\":" + g.getId());
			sb.append(",\"gname\":" + "\"" + g.getGname() + "\"");
			sb.append("}");
			if (i < list.size() - 1)
				sb.append(",");
		}
		sb.append("]");
		response.getWriter().print(sb);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
